package cn.itheima.controller.dept;

import cn.itheima.controller.base.BaseComtroller;
import cn.itheima.domain.Dept;
import cn.itheima.service.dept.DeptService;
import cn.itheima.util.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/system/dept")
public class DeptController extends BaseComtroller {

    @Autowired
    private DeptService deptService;

    @RequestMapping(name = "查询企业",value = "/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){

        PageInfo pb = deptService.findAll(pageNum,pageSize,company_id);
       request.setAttribute("pb",pb);

        return "/system/dept/dept-list";
    }

    @RequestMapping(name = "返回部门信息",value = "/toAdd")
    public String toAdd(){
        List<Dept> list = deptService.toAdd(company_id);
        request.setAttribute("deptList",list);
        return "/system/dept/dept-add";
    }
    @RequestMapping(name = "添加部门和修改部门",value = "/edit")
        public String edit(Dept dept){
        dept.setCompanyId(company_id);
        dept.setCompanyName(company_name);
        if (UtilFuns.isEmpty(dept.getId())){
            String s = UUID.randomUUID().toString();
            dept.setId(s);
            deptService.edit(dept);
        }else {
            deptService.update(dept);

        }

        return "redirect:/system/dept/list.do";
    }
    @RequestMapping(name = "删除部门",value = "/delete")
    public String delete(String id){
        deptService.delete(id);
        return "redirect:/system/dept/list.do";
    }
    @RequestMapping(name = "修改的回显",value = "/toUpdate")
    public String toUpdate(String id){
        List<Dept> list = deptService.toAdd(company_id);
        request.setAttribute("deptList",list);
        Dept dept = deptService.findById(id);
        request.setAttribute("dept",dept);
        return "/system/dept/dept-update";
    }

}
